package net.lovesrc.allin.biz.lbc;

import com.alibaba.fastjson.JSONObject;
import com.zhouzhou.fastjava.util.ThreadKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author clg
 * Created at 8:54 2019/2/25
 * @since 1.1
 */
@Slf4j
@Profile(value = "gelic")
@Service
public class LbcStrategyService extends BaseLbcService {

    @Autowired
    private LbcRegisterService lbcRegisterService;

    @Autowired
    private LbcService lbcService;

    ExecutorService executorService = Executors.newFixedThreadPool(1);

    public void calcDepth() {
        jdbcTemplate.update("update t_lbc_account set depth = 1 where promotionCode in (" +
                "select promotionCode from (" +
                "select promotionCode from t_lbc_account a where not exists (" +
                "select promotionCode from t_lbc_account b where a.promotionCode = b.inviteCode)) c)" +
                "and depth != 1");

        jdbcTemplate.update("update t_lbc_account set depth = 2 where promotionCode in (" +
                "select inviteCode from (" +
                "select inviteCode from t_lbc_account where depth = 1) c)" +
                "and depth != 2");

        jdbcTemplate.update("update t_lbc_account set depth = 3 where promotionCode in (" +
                "select inviteCode from (" +
                "select inviteCode from t_lbc_account where depth = 2) c)" +
                "and depth != 3");

        jdbcTemplate.update("update t_lbc_account set depth = 4 where promotionCode in (" +
                "select inviteCode from (" +
                "select inviteCode from t_lbc_account where depth = 3) c)" +
                "and depth != 4");

        jdbcTemplate.update("update t_lbc_account set depth = 5 where promotionCode in (" +
                "select inviteCode from (" +
                "select inviteCode from t_lbc_account where depth = 4) c)" +
                "and depth != 5");

        jdbcTemplate.update("update t_lbc_account set depth = 6 where promotionCode in (" +
                "select inviteCode from (" +
                "select inviteCode from t_lbc_account where depth = 5) c)" +
                "and depth != 6");

        jdbcTemplate.update("update t_lbc_account set depth = 7 where promotionCode in (" +
                "select inviteCode from (" +
                "select inviteCode from t_lbc_account where depth = 6) c)" +
                "and depth != 7");

        jdbcTemplate.update("update t_lbc_account set depth = 8 where promotionCode in (" +
                "select inviteCode from (" +
                "select inviteCode from t_lbc_account where depth = 7) c)" +
                "and depth != 8");

        jdbcTemplate.update("update t_lbc_account set depth = 9 where promotionCode in (" +
                "select inviteCode from (" +
                "select inviteCode from t_lbc_account where depth = 8) c)" +
                "and depth != 9");

        jdbcTemplate.update("update t_lbc_account set depth = 10 where promotionCode in (" +
                "select inviteCode from (" +
                "select inviteCode from t_lbc_account where depth = 9) c)" +
                "and depth != 10");
    }

    public void deal() {
//        jdbcTemplate.update("update t_lbc_account set mark = 'bigTree' where inviteCode = '32445548'");
//        jdbcTemplate.update("update t_lbc_account set mark = 'bigTree' where promotionCode = '32445548'");
//
//        for (;;) {
//            int update = jdbcTemplate.update("update t_lbc_account set mark = 'bigTree' where mark is null and promotionCode in (select inviteCode from (select inviteCode from t_lbc_account where mark='bigTree') a)");
//            if (update <= 0) {
//                return;
//            }
//        }
        List<JSONObject> rows = queryForRows(jdbcTemplate, "select mobile,inviteCode,promotionCode from t_lbc_account");
        Map<String, String> imMap = new HashMap<>();
        Map<String, String> ipMap = new HashMap<>();
        Map<String, Integer[]> params = new HashMap<>();
        rows.stream()
                .filter(row -> StringUtils.isNotBlank(row.getString("inviteCode")) && StringUtils.isNotBlank(row.getString("promotionCode")))
                .forEach(row -> {
                    String inviteCode = row.getString("inviteCode");
                    String promotionCode = row.getString("promotionCode");
                    String mobile = row.getString("mobile");
                    imMap.put(inviteCode, mobile);
                    ipMap.put(inviteCode, promotionCode);
                });
    }

    static int count = 0;

//    @PostConstruct
    public void execute() {
        calcDepth();
        executorService.execute(() -> {
            JSONObject resJson = queryForRow(jdbcTemplate, " select count(1) from t_job_task where type = 'LBC_NEW_REGISTER' and status in ('ING', 'NEW')");
            System.out.println(resJson);
            List<JSONObject> rows = queryForRows(jdbcTemplate, "select inviteCode " +
                    "from t_lbc_account where mark is null and CAST(inviteCode AS SIGNED INTEGER) % depth > directRCount and depth < 10 " +
                    "and depth > 0 and datediff(now(), insertTime) >=1 and mark is null order by depth");
            for (JSONObject row : rows) {
                if (count++ > 50) {
                    continue;
                }
                String inviteCode = row.getString("inviteCode");
                if (StringUtils.isBlank(inviteCode)) {
                    return;
                }
                JSONObject jsonObject = new JSONObject()
                        .fluentPut("type", "LBC_NEW_REGISTER")
                        .fluentPut("biz", new JSONObject().fluentPut("promotionCode", inviteCode).toJSONString())
                        .fluentPut("status", "NEW")
                        .fluentPut("done", 0);
                insertRow(jdbcTemplate, "t_job_task", jsonObject);
                ThreadKit.sleepMilliseconds(randMillSeconds(10 * 60));
            }
        });
    }

    private long randMillSeconds(int seconds) {
        double v = new Random().nextDouble();
        return (int) (v * seconds * 1000);
    }

}
